#include "widget.h"
#include "ui_widget.h"
#include "QMessageBox"
#include <QMouseEvent>
#include "QCloseEvent"
#include "tchar.h"
Widget::Widget(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint);//去掉标题栏
    ui->tabWidget->setCurrentIndex(1);//默认展示 显示界面
    //this->setHidden(true);
    //ui->tab->show();
    //this->setAttribute(Qt::WA_TranslucentBackground, true);//设置透明2-窗体标题栏不透明,背景透明
    //分配空间
    thread = new MyThread();
    connect(thread, SIGNAL(timeupdata(QString)), this, SLOT(timeshow(QString)), Qt::QueuedConnection);
    thread->start();
}
// 程序开机自动启动
void setAutostart()
{
    HKEY hKey;
    QString strRegPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";

    if (RegOpenKeyEx(HKEY_CURRENT_USER,
                     L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
                     0,
                     KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS){
        TCHAR strExeFullDir[MAX_PATH];
        GetModuleFileName(NULL, strExeFullDir, MAX_PATH);

        TCHAR strDir[MAX_PATH] = {};
        DWORD nLength = MAX_PATH;
        // back_time 是需要设置开机自启动应用程序的名称
        long result = RegGetValue(hKey, nullptr, L"back_time", RRF_RT_REG_SZ, 0, strDir, &nLength);

        if (result != ERROR_SUCCESS || _tcscmp(strExeFullDir, strDir) != 0)
        {
            RegSetValueEx(hKey, L"back_time", 0, REG_SZ, (LPBYTE)strExeFullDir, (lstrlen(strExeFullDir) + 1)*sizeof(TCHAR));

            RegCloseKey(hKey);
        }
    }else{
        QMessageBox::warning(0, QString::fromLocal8Bit("警告"), QString::fromLocal8Bit("\n系统参数错误,不能随系统启动n"));
    }
}

// 取消开机自动启动
void cancelAutoStart()
{
        HKEY hKey;
        QString strRegPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run";
        if (RegOpenKeyEx(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS, &hKey) == ERROR_SUCCESS){
            RegDeleteValue(hKey, L"back_time");
            RegCloseKey(hKey);
        }
}
// 设置壁纸
void setWallpaper(int num)
{
    //壁纸注册表表
    QSettings wallPaper("HKEY_CURRENT_USER\\Control Panel\\Desktop",QSettings::NativeFormat);
    //新的桌面图片路径
    QString path = QString(qApp->applicationDirPath() + "/Image/%1.jpg").arg(num);
    //设置壁纸注册表
    wallPaper.setValue("Wallpaper",path);
    QByteArray byte = path.toLocal8Bit();
    //调用windowsAPI
    SystemParametersInfoA(SPI_SETDESKWALLPAPER, 0, byte.data(), SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);


}

void Widget::closeEvent(QCloseEvent *event)
{
    //写ini文件，记录当前窗口位置和大小：
    QString wstrFilePath = qApp->applicationDirPath() + "/init.ini" ;//in windows，init.ini放在工程源文件目录下
    QSettings *settings=new QSettings(wstrFilePath,QSettings::IniFormat);//用QSetting获取ini文件中的数据
    settings->clear();//清空当前配置文件中的内容
    settings->setValue("Setting/x",this->x());
    settings->setValue("Setting/y",this->y()+38);
    settings->setValue("Setting/date",setting_date);
    settings->setValue("Setting/auto_start",auto_start);
    settings->setValue("Setting/lock_state",lock_state);
    settings->setValue("Setting/MessageBox_state",MessageBox_state);
    settings->setValue("Setting/Change_wallpaper",Change_wallpaper);
}
void Widget::setting_date_updata(QString setting_date,bool start_st,bool lock_st,bool MessageBox_st, bool Change_wallpaper_st)
{
    QDate date = QDate::currentDate();
    if(setting_date.isEmpty())
    {
        ui->dateEdit->setDate(date);
    }else {
        date = QDate::fromString(setting_date, "yyyy-MM-dd");
        qDebug()<<date;
        ui->dateEdit->setDate(date);
    }

    if(start_st)
        ui->checkBox->setCheckState(Qt::Checked);
    else {
        ui->checkBox->setCheckState(Qt::Unchecked);
    }
    auto_start = start_st;
    if(lock_st)
        ui->checkBox_2->setCheckState(Qt::Checked);
    else {
        ui->checkBox_2->setCheckState(Qt::Unchecked);
    }
    lock_state = lock_st;
    if(MessageBox_st)
        ui->checkBox_3->setCheckState(Qt::Checked);
    else {
        ui->checkBox_3->setCheckState(Qt::Unchecked);
    }
    MessageBox_state = MessageBox_st;
    if(Change_wallpaper_st)
        ui->checkBox_4->setCheckState(Qt::Checked);
    else {
        ui->checkBox_4->setCheckState(Qt::Unchecked);
    }
    Change_wallpaper = Change_wallpaper_st;
}
void Widget::timeshow(QString str)
{

    int old_hour;
    QDate Date = QDate::currentDate();
    int year = Date.year();
    int month = Date.month();
    int day = Date.day();
    bool ok = true;\
    old_hour = hour;
    hour = str.mid(11,2).toInt(&ok,10);
    qDebug()<<hour;
    int back_year;
    int back_month;
    int back_day;
    int show_day;
    if((Change_wallpaper==true)&&(old_hour!=hour))
    {
        setWallpaper(hour);
    }
    back_year = set_year - year;
    back_month = set_month - month;
    back_day = set_day - day;

    show_day = back_year*365+back_month*30+back_day;
    QString day_show = QString::number(show_day);
    ui->label_4->setText(day_show);
    ui->label->setText(str);
}
Widget::~Widget()
{
    delete ui;
}
void Widget::on_dateEdit_userDateChanged(const QDate &date)
{
    set_year = date.year();
    set_month = date.month();
    set_day = date.day();
    //获取编辑区内容
    setting_date = date.toString("yyyy-MM-dd");
}
//开机自启
void Widget::on_checkBox_stateChanged(int arg1)
{
    auto_start = arg1;
    qDebug()<<arg1;
    if(auto_start)
    {
        setAutostart();
    }else {
        cancelAutoStart();
    }
}
//锁定
void Widget::on_checkBox_2_stateChanged(int arg1)
{
    lock_state = arg1;
    if(arg1)
    {
        lock_state = true;  //锁定
    }else {
        lock_state = false; //解锁
    }
}
//锁定后弹窗
void Widget::on_checkBox_3_stateChanged(int arg1)
{
    MessageBox_state = arg1;
    if(arg1)
    {
        MessageBox_state = true;  //有弹窗提醒
    }else {
        MessageBox_state = false; //无弹窗提醒
    }
}
//随时间自动切换壁纸
void Widget::on_checkBox_4_stateChanged(int arg1)
{
    Change_wallpaper = arg1;
}
//重新鼠标按键事件
void Widget::mousePressEvent(QMouseEvent *event)
{
    if (event->button() == Qt::LeftButton)
        last_mouse_position_ = event->globalPos();
}
//重写鼠标移动事件
void Widget::mouseMoveEvent(QMouseEvent *event)
{
    if (!event->buttons().testFlag(Qt::LeftButton))
            return;
    const QPoint position = pos() + event->globalPos() - last_mouse_position_; //the position of mainfrmae + (current_mouse_position - last_mouse_position)
    if(lock_state == false)
    {
        move(position.x(), position.y());
    }else {
        if(MessageBox_state == true)
        {
            QMessageBox::information(NULL, "提示", "请先解锁");
        }
    }

    last_mouse_position_ = event->globalPos();
}


